<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">
<!--当前表名：${table.name}-->
<#assign prefix><#list '${table.name}' ?split('_') as name >${'${name}'?substring(0,1)}</#list>.</#assign>
<#assign instanceOf="com.sbtr.common.auto.InstanceOfMethod"?new()/>
<#if !cfg.usePrefix>
    <#assign prefix></#assign>
</#if>
<#if baseResultMap>
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
<#list table.fields as field>
<#if field.keyFlag><#--生成主键排在第一位-->
        <id column="${field.name}" property="${field.propertyName}" />
</#if>
</#list>
<#list table.commonFields as field><#--生成公共字段 -->
    <result column="${field.name}" property="${field.propertyName}" />
</#list>
<#list table.fields as field>
<#if !field.keyFlag><#--生成普通字段 -->
        <result column="${field.name}" property="${field.propertyName}" />
</#if>
</#list>
    </resultMap>

</#if>
<#if baseColumnList>
    <!-- 通用查询结果列 -->
    <sql id="Base_Column_SQL">
    <#list table.commonFields as field>${prefix}${field.name},</#list>${prefix}${'${table.fieldNames}'?replace(", ",",${prefix}")}
    </sql>
</#if>

    <sql id="whereParam">
        <if test="params!=null">
    <#list table.fields as field>
        <#if instanceOf('${field.propertyType}')>
            <if test="@org.apache.commons.lang3.StringUtils@isNotBlank(params.${field.propertyName})"> and ${prefix}${field.name} = ${r"#"}{params.${field.propertyName}}</if>
        <#else>
            <if test="params.${field.propertyName}!=null"> and ${prefix}${field.name} = ${r"#"}{params.${field.propertyName}}</if>
        </#if>
    </#list>
        </if>
    </sql>

    <sql id="whereParam_entity">
    <#list table.fields as field>
        <#if instanceOf('${field.propertyType}')>
            <if test="@org.apache.commons.lang3.StringUtils@isNotBlank(${field.propertyName})"> and ${prefix}${field.name} = ${r"#"}{${field.propertyName}}</if>
        <#else>
            <if test="${field.propertyName}!=null"> and ${prefix}${field.name} = ${r"#"}{${field.propertyName}}</if>
        </#if>
    </#list>
    </sql>


    <select id="selectPages" resultType="${package.Entity}.${entity}">
        select <include refid="Base_Column_SQL"/> from ${table.name} ${'${prefix}'?replace(".","")}
        <where>
            <include refid="whereParam"/>
        </where>
    </select>

</mapper>
